package com.glink.manage.controller;

import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.common.CommonUtils;
import com.glink.manage.dto.menu.*;
import com.glink.manage.service.MenuService;
import com.glink.manage.service.RoleMenuRelaService;
import com.glink.manage.vo.menu.MenuBranchVO;
import com.glink.manage.vo.menu.MenuFunctionInfoVO;
import com.glink.manage.vo.menu.MenuInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/API/Manage/menu")
@Api(tags = "菜单管理")
public class MenuController extends AbstractBaseController {

    @Autowired
    private MenuService menuService;
    
    @Autowired
    private RoleMenuRelaService roleMenuRelaService;
    
    @ApiOperation(value = "01.1 新增菜单")
    @PostMapping(value = "/insertMenu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<MenuInfoVO> insertMenu(@Validated @RequestBody MenuSaveRequestDTO dto) throws GeneralSimpleException {
        log.info("01.1 新增权限菜单：{}", LogUtil.encode(dto));
        MenuInfoVO menuInfoVO = menuService.insertMenu(dto);
        log.info("01.1 新增权限菜单：success");
        return this.response(menuInfoVO);
    }

    @ApiOperation(value = "01.2 修改菜单")
    @PostMapping(value = "/updateMenu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<MenuInfoVO> updateMenu(@Validated @RequestBody MenuUpdateRequestDTO dto) throws GeneralSimpleException {
        log.info("01.2 修改权限菜单：{}", LogUtil.encode(dto));
        MenuInfoVO menuInfoVO = menuService.updateMenu(dto);
        log.info("01.2 修改权限菜单：success");
        return this.response(menuInfoVO);
    }

    @ApiOperation(value = "02.1 新增功能点")
    @PostMapping(value = "/insertFunctionPoints", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<MenuFunctionInfoVO> insertFunctionPoints(@Validated @RequestBody MenuFunctionPointsSaveRequestDTO elementRequestVO) throws GeneralSimpleException {
        log.info("02.1 新增功能点：{}", LogUtil.encode(elementRequestVO));
        MenuFunctionInfoVO menuInfoVO = menuService.insertFunctionPoints(elementRequestVO);
        log.info("02.1 新增功能点：success");
        return this.response(menuInfoVO);
    }

    @ApiOperation(value = "02.2 更新功能点")
    @PostMapping(value = "/updateFunctionPoints", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<MenuFunctionInfoVO> updateFunctionPoints(@Validated @RequestBody MenuFunctionPointsUpdateRequestDTO elementRequestVO) throws GeneralSimpleException {
        log.info("02.2 更新功能点：{}", LogUtil.encode(elementRequestVO));
        MenuFunctionInfoVO menuInfoVO = menuService.updateFunctionPoints(elementRequestVO);
        log.info("02.2 更新功能点：success");
        return this.response(menuInfoVO);
    }
    
    @ApiOperation(value = "03.根据权限菜单id查询")
    @PostMapping(value = "/findMenu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<MenuInfoVO> getMenuById(@Validated @RequestBody MenuIdRequestDTO requestVO) {
        log.info("03.根据权限菜单id查询：{}", LogUtil.encode(requestVO));
        MenuInfoVO menuInfoVO = menuService.getMenuInfoById(requestVO.getMenuId());
        log.info("03.根据权限菜单id查询：success");
        return this.response(menuInfoVO);
    }

    @ApiOperation(value = "04.查询菜单列表所有-全部菜单树")
    @GetMapping(value = "/findMenuList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<MenuInfoVO>> getMenuList() {
        log.info("04.查询菜单列表所有-全部菜单树");
        List<MenuInfoVO> resourceList = menuService.getMenuList();
        log.info("04.查询菜单列表所有-全部菜单树：success");
        return this.response(resourceList);
    }

//    @ApiOperation(value = "5.权限菜单列表分页")
//    @PostMapping(value = "/findMenuPage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public ResponseResult<IPage<MenuInfoVO>> getResourcePage(@Validated @RequestBody MenuPageRequestDTO pageRequestVO) {
//        log.info("5.权限菜单列表分页 pageRequestVO：{}", LogUtil.encode(pageRequestVO));
//        IPage<MenuInfoVO> iPage = menuService.getMenuPage(pageRequestVO);
//        log.info("5.权限菜单列表分页 ：success");
//        return this.response(iPage);
//    }

    @ApiOperation(value = "06.根据菜单类型获取菜单或元素(字典MenuResourceType):01-菜单,02-按钮,03-页面快捷方式,04-首页小面板")
    @PostMapping(value = "/findByType", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<MenuBranchVO>> findAllMenuList(@Validated @RequestBody MenuTypeDTO typeDTO) {
        log.info("06.根据菜单类型获取菜单或元素：{}", LogUtil.encode(typeDTO));
        List<MenuBranchVO> menuList = menuService.findAllMenuListByType(typeDTO.getMenuType());
        log.info("06.根据菜单类型获取菜单或元素：success");
        return this.response(menuList);
    }

    @ApiOperation(value = "06.1 获取被授权的菜单或元素")
    @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的用户id", dataType = "string", required = true)
    @GetMapping(value = "/getAuthMenu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<MenuBranchVO>> getAuthMenu() throws GeneralSimpleException {
        log.info("06.1 获取所有菜单或元素");
        String userId = CommonUtils.extractUserIdtByToken();
        List<MenuBranchVO> menuList = roleMenuRelaService.getAuthMenu(userId);
        log.info("06.1 获取所有菜单或元素：success");
        return this.response(menuList);
    }

    @ApiOperation(value = "07.根据菜单ID获取菜单下所有的按钮")
    @PostMapping(value = "/findButton", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<MenuFunctionInfoVO>> getButtonList(@Validated @RequestBody MenuIdRequestDTO requestVO) {
        log.info("07.根据菜单ID获取菜单下所有的按钮：{}", LogUtil.encode(requestVO));
        List<MenuFunctionInfoVO> buttonList = menuService.getButtonList(requestVO.getMenuId());
        log.info("07.根据菜单ID获取菜单下所有的按钮：success");
        return this.response(buttonList);
    }

    @ApiOperation(value = "08.根据菜单id删除")
    @PostMapping(value = "/deleteById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Integer> deleteMenuById(@Validated @RequestBody MenuIdRequestDTO requestVO) throws GeneralSimpleException {
        log.info("08.根据菜单id删除：{}", LogUtil.encode(requestVO));
        int i = menuService.deleteResourceById(requestVO.getMenuId());
        log.info("08.根据菜单id删除：success");
        return this.response(i);
    }
}
